package com.xiaoyu.izf.service;

import com.xiaoyu.izf.model.NewsInfo;
import com.xiaoyu.izf.repository.NewsInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

@Service
public class NewsInfoService extends BaseService {
    @Autowired
    private NewsInfoDao newsInfoDao;

    public NewsInfo newsOne(int id){
        return newsInfoDao.findOneById(id);
    }

    public Page<NewsInfo> listForApi(int p, int psize){
        PageRequest pageRequest = buildPageRequest(p, psize, "addTime");
        Specification<NewsInfo> specification = new Specification<NewsInfo>() {
            @Override
            public Predicate toPredicate(Root<NewsInfo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.equal(root.get("isDel"), 0);
                return predicate;
            }
        };
        Page<NewsInfo> page = newsInfoDao.findAll(specification,  pageRequest);
        return page;
        //return page.getContent();
    }
}
